import { Uri } from "vscode";
import { IExtensionApi } from "./apiTypes";
import { isTestExecution } from "./common/constants";
import { IConfigurationService } from "./common/types";
import { IInterpreterService } from "./interpreter/contracts";
import { IServiceContainer, IServiceManager } from "./ioc/types";
import { traceError } from "./logging";

export function buildApi(
    ready: Promise<any>,
    serviceManager: IServiceManager,
    serviceContainer: IServiceContainer,
): IExtensionApi {

    const configurationService = serviceContainer.get<IConfigurationService>(IConfigurationService);
    const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
    // serviceManager.addSingleton<JupyterExtensionIntegration>(JupyterExtensionIntegration, JupyterExtensionIntegration);
    // const jupyterIntegration = serviceContainer.get<JupyterExtensionIntegration>(JupyterExtensionIntegration);
    const api: IExtensionApi = {
        // 'ready' will propagate the exception, but we must log it here first.
        ready: ready.catch((ex) => {
            traceError('Failure during activation.', ex);
            return Promise.reject(ex);
        }),
        jupyter: {
            registerHooks: function (): void {
                throw new Error("Function not implemented.");
            }
        },
        settings: {
            onDidChangeExecutionDetails: interpreterService.onDidChangeInterpreterConfiguration,
            getExecutionDetails: function (resource?: Uri): { execCommand: string[] | undefined; } {
                throw new Error("Function not implemented.");
            }
        },
        datascience: {},
        debug:{
            async getRemoteLauncherCommand(
                host: string,
                port: number,
                waitUntilDebuggerAttaches: boolean = true,
            ): Promise<string[]> {
                throw new Error("Function not implemented.");
            },
            async getDebuggerPackagePath(): Promise<string | undefined> {
                throw new Error("Function not implemented.");
            },
        }
    };

    // In test environment return the DI Container.
    if (isTestExecution()) {
        (api as any).serviceContainer = serviceContainer;
        (api as any).serviceManager = serviceManager;
    }

    return api;
}